<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
  <meta name="theme-color" content="#222">
  <meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

  <link rel="stylesheet" href="/css/main.css">



  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

  <script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {
      "hostname": "holidaypenguin.gitee.io",
      "root": "/",
      "images": "/images",
      "scheme": "Mist",
      "version": "8.2.2",
      "exturl": false,
      "sidebar": {
        "position": "right",
        "display": "always",
        "padding": 18,
        "offset": 12
      },
      "copycode": true,
      "bookmark": {
        "enable": false,
        "color": "#222",
        "save": "auto"
      },
      "fancybox": false,
      "mediumzoom": false,
      "lazyload": false,
      "pangu": false,
      "comments": {
        "style": "tabs",
        "active": null,
        "storage": true,
        "lazyload": false,
        "nav": null
      },
      "motion": {
        "enable": true,
        "async": true,
        "transition": {
          "post_block": "fadeIn",
          "post_header": "fadeInDown",
          "post_body": "fadeInDown",
          "coll_header": "fadeInLeft",
          "sidebar": "slideUpIn"
        }
      },
      "prism": false,
      "i18n": {
        "placeholder": "搜索...",
        "empty": "没有找到任何搜索结果：${query}",
        "hits_time": "找到 ${hits} 个搜索结果（用时 ${time} 毫秒）",
        "hits": "找到 ${hits} 个搜索结果"
      },
      "path": "/search.xml",
      "localsearch": {
        "enable": true,
        "trigger": "auto",
        "top_n_per_article": 1,
        "unescape": false,
        "preload": false
      }
    };
  </script>
  <meta property="og:type" content="article">
  <meta property="og:title" content="小程序代码命令行自动上传">
  <meta property="og:url" content="https://holidaypenguin.gitee.io/blob/2021-05-31-automatic-upload-of-small-program-code-command-line/index.html">
  <meta property="og:site_name" content="HolidayPenguin">
  <meta property="og:locale" content="zh_CN">
  <meta property="og:image" content="https://visitor-badge.glitch.me/badge?page_id=holidaypenguin.gitee.io/">
  <meta property="article:published_time" content="2021-05-31T06:29:11.000Z">
  <meta property="article:modified_time" content="2021-05-31T06:29:11.000Z">
  <meta property="article:author" content="holidaypenguin">
  <meta property="article:tag" content="微信小程序">
  <meta property="article:tag" content="开发者工具">
  <meta property="article:tag" content="代码上传">
  <meta property="article:tag" content="命令行调用">
  <meta property="article:tag" content="miniprogram-ci">
  <meta name="twitter:card" content="summary">
  <meta name="twitter:image" content="https://visitor-badge.glitch.me/badge?page_id=holidaypenguin.gitee.io/">


  <link rel="canonical" href="https://holidaypenguin.gitee.io/blob/2021-05-31-automatic-upload-of-small-program-code-command-line/">


  <script class="page-configurations">
    // https://hexo.io/docs/variables.html
    CONFIG.page = {
      sidebar: "",
      isHome: false,
      isPost: true,
      lang: 'zh-CN'
    };
  </script>
  <title>小程序代码命令行自动上传 | HolidayPenguin</title>





  <noscript>
    <style>
      body {
        margin-top: 2rem;
      }

      .use-motion .menu-item,
      .use-motion .sidebar,
      .use-motion .post-block,
      .use-motion .pagination,
      .use-motion .comments,
      .use-motion .post-header,
      .use-motion .post-body,
      .use-motion .collection-header {
        visibility: visible;
      }

      .use-motion .header,
      .use-motion .site-brand-container .toggle,
      .use-motion .footer {
        opacity: initial;
      }

      .use-motion .site-title,
      .use-motion .site-subtitle,
      .use-motion .custom-logo-image {
        opacity: initial;
        top: initial;
      }

      .use-motion .logo-line {
        transform: scaleX(1);
      }

      .search-pop-overlay,
      .sidebar-nav {
        display: none;
      }

      .sidebar-panel {
        display: block;
      }
    </style>
  </noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner">
        <div class="site-brand-container">
          <div class="site-nav-toggle">
            <div class="toggle" aria-label="切换导航栏" role="button">
              <span class="toggle-line"></span>
              <span class="toggle-line"></span>
              <span class="toggle-line"></span>
            </div>
          </div>

          <div class="site-meta">

            <a href="/" class="brand" rel="start">
              <i class="logo-line"></i>
              <h1 class="site-title">HolidayPenguin</h1>
              <i class="logo-line"></i>
            </a>
          </div>

          <div class="site-nav-right">
            <div class="toggle popup-trigger">
              <i class="fa fa-search fa-fw fa-lg"></i>
            </div>
          </div>
        </div>



        <nav class="site-nav">
          <ul class="main-menu menu">
            <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
            <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li>
            <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
            <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
            <li class="menu-item menu-item-search">
              <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
              </a>
            </li>
          </ul>
        </nav>



        <div class="search-pop-overlay">
          <div class="popup search-popup">
            <div class="search-header">
              <span class="search-icon">
                <i class="fa fa-search"></i>
              </span>
              <div class="search-input-container">
                <input autocomplete="off" autocapitalize="off" maxlength="80" placeholder="搜索..." spellcheck="false" type="search" class="search-input">
              </div>
              <span class="popup-btn-close" role="button">
                <i class="fa fa-times-circle"></i>
              </span>
            </div>
            <div class="search-result-container no-result">
              <div class="search-result-icon">
                <i class="fa fa-spinner fa-pulse fa-5x"></i>
              </div>
            </div>

          </div>
        </div>

      </div>


      <div class="toggle sidebar-toggle" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
      </div>

      <aside class="sidebar">

        <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
          <ul class="sidebar-nav">
            <li class="sidebar-nav-toc">
              文章目录
            </li>
            <li class="sidebar-nav-overview">
              站点概览
            </li>
          </ul>

          <div class="sidebar-panel-container">
            <!--noindex-->
            <div class="post-toc-wrap sidebar-panel">
              <div class="post-toc animated">
                <ol class="nav">
                  <li class="nav-item nav-level-1"><a class="nav-link" href="#%E8%BF%90%E8%A1%8C%E5%91%BD%E4%BB%A4"><span class="nav-number">1.</span> <span class="nav-text">运行命令</span></a></li>
                  <li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BB%A3%E7%A0%81%E5%88%86%E8%A7%A3"><span class="nav-number">2.</span> <span class="nav-text">代码分解</span></a>
                    <ol class="nav-child">
                      <li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%96%87%E4%BB%B6%E8%AF%B4%E6%98%8E"><span class="nav-number">2.1.</span> <span class="nav-text">文件说明</span></a></li>
                      <li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%8E%B7%E5%8F%96%E5%BC%80%E5%8F%91%E8%80%85%E5%B7%A5%E5%85%B7%E7%9B%AE%E5%BD%95"><span class="nav-number">2.2.</span> <span class="nav-text">获取开发者工具目录</span></a></li>
                      <li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%80%89%E6%8B%A9%E7%BC%96%E8%AF%91%E7%8E%AF%E5%A2%83"><span class="nav-number">2.3.</span> <span class="nav-text">选择编译环境</span></a></li>
                      <li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B0%8F%E7%A8%8B%E5%BA%8F%E4%BB%A3%E7%A0%81%E4%B8%8A%E4%BC%A0"><span class="nav-number">2.4.</span> <span class="nav-text">小程序代码上传</span></a></li>
                      <li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B0%8F%E7%A8%8B%E5%BA%8F%E4%BB%A3%E7%A0%81%E9%9D%99%E9%BB%98%E4%B8%8A%E4%BC%A0"><span class="nav-number">2.5.</span> <span class="nav-text">小程序代码静默上传</span></a></li>
                      <li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%8E%B7%E5%8F%96%E4%B8%8A%E4%BC%A0%E7%89%88%E6%9C%AC%E5%8F%B7"><span class="nav-number">2.6.</span> <span class="nav-text">获取上传版本号</span></a></li>
                      <li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9C%AC%E5%9C%B0%E5%86%99%E5%85%A5%E7%89%88%E6%9C%AC%E5%8F%B7"><span class="nav-number">2.7.</span> <span class="nav-text">本地写入版本号</span></a></li>
                    </ol>
                  </li>
                  <li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BB%A3%E7%A0%81%E5%90%88%E9%9B%86"><span class="nav-number">3.</span> <span class="nav-text">代码合集</span></a></li>
                  <li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%B3%A8%E6%84%8F"><span class="nav-number">4.</span> <span class="nav-text">注意</span></a></li>
                  <li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%BC%96%E8%AF%91%E9%83%A8%E5%88%86%E4%BF%AE%E6%94%B9"><span class="nav-number">5.</span> <span class="nav-text">编译部分修改</span></a></li>
                  <li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8F%82%E8%80%83"><span class="nav-number">6.</span> <span class="nav-text">参考</span></a></li>
                </ol>
              </div>
            </div>
            <!--/noindex-->

            <div class="site-overview-wrap sidebar-panel">
              <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
                <p class="site-author-name" itemprop="name">holidaypenguin</p>
                <div class="site-description" itemprop="description"></div>
              </div>
              <div class="site-state-wrap site-overview-item animated">
                <nav class="site-state">
                  <div class="site-state-item site-state-posts">
                    <a href="/archives/">

                      <span class="site-state-item-count">138</span>
                      <span class="site-state-item-name">日志</span>
                    </a>
                  </div>
                  <div class="site-state-item site-state-categories">
                    <a href="/categories/">

                      <span class="site-state-item-count">26</span>
                      <span class="site-state-item-name">分类</span></a>
                  </div>
                  <div class="site-state-item site-state-tags">
                    <a href="/tags/">

                      <span class="site-state-item-count">234</span>
                      <span class="site-state-item-name">标签</span></a>
                  </div>
                </nav>
              </div>
              <div class="links-of-author site-overview-item animated">
                <span class="links-of-author-item">
                  <a href="https://github.com/holidaypenguin" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;holidaypenguin" rel="noopener" target="_blank"><i class="github fa-fw"></i>GitHub</a>
                </span>
                <span class="links-of-author-item">
                  <a href="mailto:songshipeng2016@gmail.com" title="E-Mail → mailto:songshipeng2016@gmail.com" rel="noopener" target="_blank"><i class="envelope fa-fw"></i>E-Mail</a>
                </span>
              </div>



            </div>
          </div>
        </div>
      </aside>
      <div class="sidebar-dimmer"></div>


    </header>


    <div class="back-to-top" role="button">
      <i class="fa fa-arrow-up"></i>
      <span>0%</span>
    </div>

    <noscript>
      <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
    </noscript>


    <div class="main-inner post posts-expand">





      <div class="post-block">



        <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
          <link itemprop="mainEntityOfPage" href="https://holidaypenguin.gitee.io/blob/2021-05-31-automatic-upload-of-small-program-code-command-line/">

          <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
            <meta itemprop="image" content="/images/avatar.gif">
            <meta itemprop="name" content="holidaypenguin">
            <meta itemprop="description" content="">
          </span>

          <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
            <meta itemprop="name" content="HolidayPenguin">
          </span>
          <header class="post-header">
            <h1 class="post-title" itemprop="name headline">
              小程序代码命令行自动上传
            </h1>

            <div class="post-meta-container">
              <div class="post-meta">
                <span class="post-meta-item">
                  <span class="post-meta-item-icon">
                    <i class="far fa-calendar"></i>
                  </span>
                  <span class="post-meta-item-text">发表于</span>

                  <time title="创建时间：2021-05-31 14:29:11" itemprop="dateCreated datePublished" datetime="2021-05-31T14:29:11+08:00">2021-05-31</time>
                </span>
                <span class="post-meta-item">
                  <span class="post-meta-item-icon">
                    <i class="far fa-folder"></i>
                  </span>
                  <span class="post-meta-item-text">分类于</span>
                  <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                    <a href="/categories/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F/" itemprop="url" rel="index"><span itemprop="name">微信小程序</span></a>
                  </span>
                </span>


                <span id="/blob/2021-05-31-automatic-upload-of-small-program-code-command-line/" class="post-meta-item leancloud_visitors" data-flag-title="小程序代码命令行自动上传" title="阅读次数">
                  <span class="post-meta-item-icon">
                    <i class="far fa-eye"></i>
                  </span>
                  <span class="post-meta-item-text">阅读次数：</span>
                  <span class="leancloud-visitors-count"></span>
                </span>
              </div>

              <div class="post-description">
                <!-- more -->
              </div>
            </div>
          </header>




          <div class="post-body" itemprop="articleBody">
            <p><a target="_blank" rel="noopener" href="https://github.com/jwenjian/visitor-count-badge"><img src="" data-original="https://visitor-badge.glitch.me/badge?page_id=holidaypenguin.gitee.io/" alt="总访客数量"></a></p>
            <blockquote>
              <p>因为开发环境上线比较多，顾虑手动修改代码过程中错误修改，导致上线后使用错误的环境，因此使用此方式</p>
            </blockquote>
            <h1 id="运行命令"><a href="#运行命令" class="headerlink" title="运行命令"></a>运行命令</h1>
            <p>编译文件目录 build/build.js</p>
            <p>设定编辑器所在目录、运行环境</p>
            <figure class="highlight plain">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line">&quot;build&quot;: &quot;node build&#x2F;build.js&quot;</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>
            <p>设定编辑器所在目录、运行环境，小程序代码上传</p>
            <figure class="highlight plain">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line">&quot;build:upload&quot;: &quot;node build&#x2F;build.js upload&quot;</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>
            <p>设定编辑器所在目录、运行环境，小程序代码上传，版本号写入</p>
            <figure class="highlight plain">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line">&quot;build:upload:version&quot;: &quot;node build&#x2F;build.js upload version&quot;</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p>这里也可以仅仅有第一条命令，就需要一段可以选择编译功能的代码，推荐使用 <a target="_blank" rel="noopener" href="https://github.com/SBoudrias/Inquirer.js">inquirer</a></p>
            <h1 id="代码分解"><a href="#代码分解" class="headerlink" title="代码分解"></a>代码分解</h1>
            <h2 id="文件说明"><a href="#文件说明" class="headerlink" title="文件说明"></a>文件说明</h2>
            <p>包含两个文件，build.js buildConfig.js。<br>build.js：编译主文件<br>buildConfig.js：编译配置文件，包含开发者工具安装目录和当前环境两个变量。因为在小程序内部没有找到读取json文件的方法，所以配置文件使用.js文件</p>
            <h2 id="获取开发者工具目录"><a href="#获取开发者工具目录" class="headerlink" title="获取开发者工具目录"></a>获取开发者工具目录</h2>
            <p>因为每个人的安装目录都是不一样的，默认的安装目录为空。第一次运行需要指定目录，并把指定的目录写入 buildConfig.js 文件，假如目录不正确就需要手动删掉，重新运行命令；配置成功后再次进入就不需要再次指定。</p>
            <p>buildConfig.js 文件默认是不存在的，需要 try catch 包裹起来，在执行 writeFileSync 的时候没有文件会自动创建文件</p>
            <figure class="highlight plain">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line">async function initBuildConfig () &#123;</span><br><span class="line">  try&#123;</span><br><span class="line">    const _buildConfig &#x3D; require(&#39;.&#x2F;buildConfig.js&#39;) || &#123;&#125;</span><br><span class="line">    buildConfig.toolsPath &#x3D; _buildConfig.toolsPath || &#39;&#39;</span><br><span class="line">    buildConfig.env &#x3D; _buildConfig.env || &#39;&#39;</span><br><span class="line">  &#125; catch (e) &#123;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function getToolsPath () &#123;</span><br><span class="line">  return new Promise((resolve, reject) &#x3D;&gt; &#123;</span><br><span class="line">    const rl &#x3D; readline.createInterface(&#123;</span><br><span class="line">      input: process.stdin,</span><br><span class="line">      output: process.stdout</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    rl.question(&#39;当前未设置开发者工具安装路径，请设置：\n\t -&gt; &#39;, (path) &#x3D;&gt; &#123;</span><br><span class="line"></span><br><span class="line">      rl.close();</span><br><span class="line"></span><br><span class="line">      fs.writeFileSync(</span><br><span class="line">        &#96;$&#123;__dirname&#125;&#x2F;buildConfig.js&#96;, </span><br><span class="line">        &#96;module.exports &#x3D; $&#123;JSON.stringify(Object.assign(buildConfig, &#123;toolsPath: path&#125;), false, 2)&#125;&#96;,</span><br><span class="line">      )</span><br><span class="line"></span><br><span class="line">      console.log(&#39;\n&#39;)</span><br><span class="line"></span><br><span class="line">      resolve(path)</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <h2 id="选择编译环境"><a href="#选择编译环境" class="headerlink" title="选择编译环境"></a>选择编译环境</h2>
            <p>使用 <a target="_blank" rel="noopener" href="https://github.com/SBoudrias/Inquirer.js">inquirer</a>完成环境的选择，并写入 buildConfig.js 文件，下次进入会默认指定上一次选择的环境</p>
            <p>当其他文件需要使用环境变量是只需要引用buildConfig.js，并使用env变量即可</p>
            <figure class="highlight plain">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line">async function selectEnv() &#123;</span><br><span class="line">  const&#123;env&#125; &#x3D; await inquirer.prompt(&#123;</span><br><span class="line">    type: &#39;list&#39;,</span><br><span class="line">    name: &#39;env&#39;,</span><br><span class="line">    message: &#39;当前编译环境：\n\t -&gt;&#39;,</span><br><span class="line">    default: buildConfig.env,</span><br><span class="line">    choices: [&#39;local&#39;, &#39;qa&#39;, &#39;test&#39;, &#39;v4&#39;, &#39;v8&#39;],</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  fs.writeFileSync(</span><br><span class="line">    &#96;$&#123;__dirname&#125;&#x2F;buildConfig.js&#96;, </span><br><span class="line">    &#96;module.exports &#x3D; $&#123;JSON.stringify(Object.assign(buildConfig, &#123;env&#125;), false, 2)&#125;&#96;,</span><br><span class="line">  )</span><br><span class="line"></span><br><span class="line">  return env</span><br><span class="line">&#125;</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <h2 id="小程序代码上传"><a href="#小程序代码上传" class="headerlink" title="小程序代码上传"></a>小程序代码上传</h2>
            <p>使用命令模式的初衷是为了不打开微信开发者工具，事实证明我想的太理想了，还是需要打开的。</p>
            <p>执行命令的返回值 stdout 在执行成功的时候才有值，stderr无论成功与否都有值，所以这里使用 stdout 判断命令执行失败。</p>
            <figure class="highlight plain">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line">function commitCode (env) &#123;</span><br><span class="line">  return new Promise((resolve, reject) &#x3D;&gt; &#123;</span><br><span class="line">    const version &#x3D; packageConfig.version</span><br><span class="line">    const projectPath &#x3D; path.resolve(__dirname, &#39;..&#x2F;&#39;)</span><br><span class="line">    console.log(&quot;\n项目路径：\n\t -&gt;&quot;, projectPath, &#39;\n&#39;)</span><br><span class="line">    </span><br><span class="line">    const toolsPath &#x3D; buildConfig.toolsPath</span><br><span class="line"></span><br><span class="line">    const command &#x3D; &#96;cli $&#123;[</span><br><span class="line">      &#39;upload&#39;,</span><br><span class="line">      &#39;--project&#39;,</span><br><span class="line">      projectPath,</span><br><span class="line">      &#39;-v&#39;,</span><br><span class="line">      version,</span><br><span class="line">      &#39;-d&#39;,</span><br><span class="line">      &#96;$&#123;env&#125;-$&#123;version&#125;&#96;</span><br><span class="line">    ].join(&#39; &#39;)&#125;&#96;</span><br><span class="line"></span><br><span class="line">    console.log(&quot;小程序上传代码命令：\n\t -&gt;&quot;, command, &#39;\n&#39;)</span><br><span class="line"></span><br><span class="line">    exec(command, &#123;cwd: toolsPath&#125;, (error, stdout, stderr) &#x3D;&gt; &#123;</span><br><span class="line">      if (error) &#123;</span><br><span class="line">        console.error(&#96;执行的错误: $&#123;error&#125;&#96;)</span><br><span class="line"></span><br><span class="line">        reject(error)</span><br><span class="line">        return;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      if (!stdout) &#123;</span><br><span class="line">        console.error(&#96;执行的错误: $&#123;stderr&#125;&#96;)</span><br><span class="line"></span><br><span class="line">        reject(stderr)</span><br><span class="line">        return;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      console.log(&#96;stdout: $&#123;stdout&#125;&#96;);</span><br><span class="line"></span><br><span class="line">      resolve()</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <h2 id="小程序代码静默上传"><a href="#小程序代码静默上传" class="headerlink" title="小程序代码静默上传"></a>小程序代码静默上传</h2>
            <p><a target="_blank" rel="noopener" href="https://www.npmjs.com/package/miniprogram-ci">miniprogram-ci</a> 是从微信开发者工具中抽离的关于小程序/小游戏项目代码的编译模块，需要代码上传密钥和配置IP白名单，同时支持 node 脚本调用方式和 命令行 调用方式。</p>
            <p>这种静默方式上传是一种更好的方式，但是我推荐使用脚本调用的方式</p>
            <p>一套很好的应用示例：<a target="_blank" rel="noopener" href="https://github.com/xlzy520/uniapp-tailwind-uview-starter">https://github.com/xlzy520/uniapp-tailwind-uview-starter</a></p>
            <h2 id="获取上传版本号"><a href="#获取上传版本号" class="headerlink" title="获取上传版本号"></a>获取上传版本号</h2>
            <p>若果没有版本号则指定默认版本号1.0.0</p>
            <p>有版本号则根据 版本升级、特性更新、修订补丁 的选择修改对应位置的数字，这里没有对版本号做过多的校验，以为错误的版本号是会报错的，没报错就是系统允许的。</p>
            <figure class="highlight plain">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line">async function getVersion () &#123;</span><br><span class="line">  if (!packageConfig.version) &#123;</span><br><span class="line">    const version &#x3D; &#39;1.0.0&#39;</span><br><span class="line"></span><br><span class="line">    console.log(&quot;\n当前无版本号，使用默认版本号：\n\t -&gt;&quot;, version)</span><br><span class="line"></span><br><span class="line">    packageConfig.version &#x3D; version</span><br><span class="line"></span><br><span class="line">    return version</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  console.log(&quot;\n当前版本号：\n\t -&gt;&quot;, packageConfig.version, &#39;\n&#39;)</span><br><span class="line"></span><br><span class="line">  const versionTypeOpts &#x3D; [&#39;版本升级&#39;, &#39;特性更新&#39;, &#39;修订补丁&#39;]</span><br><span class="line"></span><br><span class="line">  const &#123;versionType&#125; &#x3D; await inquirer.prompt(&#123;</span><br><span class="line">    type: &#39;list&#39;,</span><br><span class="line">    name: &#39;versionType&#39;,</span><br><span class="line">    message: &#39;选择需要更新的版本类型：\n\t -&gt;&#39;,</span><br><span class="line">    default: &#39;修订补丁&#39;,</span><br><span class="line">    choices: versionTypeOpts,</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  const index &#x3D; versionTypeOpts.findIndex(item &#x3D;&gt; item &#x3D;&#x3D;&#x3D; versionType)</span><br><span class="line"></span><br><span class="line">  const version &#x3D; packageConfig.version.replace(&#x2F;(.+)\.(.+)\.(.+)&#x2F;, (...args) &#x3D;&gt; &#123;</span><br><span class="line">    const _version &#x3D; args.slice(1,4)</span><br><span class="line">    _version[index] &#x3D; Number(_version[index] || 0) + 1</span><br><span class="line"></span><br><span class="line">    return _version.join(&#39;.&#39;)</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  packageConfig.version &#x3D; version</span><br><span class="line"></span><br><span class="line">  return version</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <p>未来版本是选择版本号+自定义版本号，比如当前是1.0.0给出如下几种方案进行选择：</p>
            <ul>
              <li>1.0.0</li>
              <li>1.0.1</li>
              <li>1.1.0</li>
              <li>2.0.0</li>
              <li>自定义</li>
            </ul>
            <p>选择自定义需要开发者自己手动输入，需要分三段输入，每次输入都要第一段校验正整数，后面两段检验是非负整数，并写入package.json</p>
            <p>这种傻瓜式配置更能让人理解，而且扩展能力也比较好</p>
            <h2 id="本地写入版本号"><a href="#本地写入版本号" class="headerlink" title="本地写入版本号"></a>本地写入版本号</h2>
            <p>写入 package.json 的 version。</p>
            <figure class="highlight plain">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line">function setVersion () &#123;</span><br><span class="line">  if (process.argv[3] !&#x3D;&#x3D; &#39;version&#39;) return</span><br><span class="line"></span><br><span class="line">  console.log(&quot;版本号写入：\n\t -&gt;&quot;, packageConfig.version, &#39;\n&#39;)</span><br><span class="line">  fs.writeFileSync(</span><br><span class="line">    path.resolve(__dirname, &#39;..&#x2F;package.json&#39;), </span><br><span class="line">    JSON.stringify(packageConfig, false, 2),</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>

            <h1 id="代码合集"><a href="#代码合集" class="headerlink" title="代码合集"></a>代码合集</h1>
            <figure class="highlight plain">
              <table>
                <tr>
                  <td class="gutter">
                    <pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br></pre>
                  </td>
                  <td class="code">
                    <pre><span class="line">const readline &#x3D; require(&#39;readline&#39;);</span><br><span class="line">const buildConfig &#x3D; require(&#39;.&#x2F;buildConfig.js&#39;)</span><br><span class="line">const packageConfig &#x3D; require(&#39;..&#x2F;package.json&#39;)</span><br><span class="line">const &#123; exec &#125; &#x3D; require(&#39;child_process&#39;)</span><br><span class="line">const fs &#x3D; require(&#39;fs&#39;)</span><br><span class="line">const path &#x3D; require(&#39;path&#39;)</span><br><span class="line">var inquirer &#x3D; require(&#39;inquirer&#39;)</span><br><span class="line"></span><br><span class="line">init()</span><br><span class="line"></span><br><span class="line">async function init () &#123;</span><br><span class="line">  &#x2F;&#x2F; console.log(Object.prototype.toString.call(buildConfig))</span><br><span class="line">  console.log(&quot;当前编译配置：\n\t -&gt;&quot;, buildConfig, &#39;\n&#39;)</span><br><span class="line">  &#x2F;&#x2F; console.log(&#39;__dirname : &#39; + __dirname)</span><br><span class="line"></span><br><span class="line">  if (!buildConfig.toolsPath) &#123;</span><br><span class="line">    await getToolsPath()</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  console.log(&quot;开发者工具安装路径：\n\t -&gt;&quot;, buildConfig.toolsPath, &#39;\n&#39;)</span><br><span class="line"></span><br><span class="line">  const env &#x3D; await selectEnv()</span><br><span class="line"></span><br><span class="line">  if (process.argv[2] !&#x3D;&#x3D; &#39;upload&#39;) return</span><br><span class="line"></span><br><span class="line">  await getVersion()</span><br><span class="line">  </span><br><span class="line">  await commitCode(env)</span><br><span class="line"></span><br><span class="line">  await setVersion()</span><br><span class="line"></span><br><span class="line">  console.log(&quot;上传成功：\n\t -&gt;&quot;, env, &#39;\n&#39;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#x2F;**</span><br><span class="line"> * 获取开发者工具目录</span><br><span class="line"> *&#x2F;</span><br><span class="line">function getToolsPath () &#123;</span><br><span class="line">  return new Promise((resolve, reject) &#x3D;&gt; &#123;</span><br><span class="line">    const rl &#x3D; readline.createInterface(&#123;</span><br><span class="line">      input: process.stdin,</span><br><span class="line">      output: process.stdout</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    rl.question(&#39;当前未设置开发者工具安装路径，请设置：\n\t -&gt; &#39;, (path) &#x3D;&gt; &#123;</span><br><span class="line"></span><br><span class="line">      rl.close();</span><br><span class="line"></span><br><span class="line">      fs.writeFileSync(</span><br><span class="line">        &#96;$&#123;__dirname&#125;&#x2F;buildConfig.js&#96;, </span><br><span class="line">        &#96;module.exports &#x3D; $&#123;JSON.stringify(Object.assign(buildConfig, &#123;toolsPath: path&#125;), false, 2)&#125;&#96;,</span><br><span class="line">      )</span><br><span class="line"></span><br><span class="line">      console.log(&#39;\n&#39;)</span><br><span class="line"></span><br><span class="line">      resolve(path)</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#x2F;**</span><br><span class="line"> * 选择编译环境</span><br><span class="line"> *&#x2F;</span><br><span class="line">async function selectEnv() &#123;</span><br><span class="line">  const&#123;env&#125; &#x3D; await inquirer.prompt(&#123;</span><br><span class="line">    type: &#39;list&#39;,</span><br><span class="line">    name: &#39;env&#39;,</span><br><span class="line">    message: &#39;当前编译环境：\n\t -&gt;&#39;,</span><br><span class="line">    default: buildConfig.env,</span><br><span class="line">    choices: [&#39;local&#39;, &#39;qa&#39;, &#39;test&#39;, &#39;v4&#39;, &#39;v8&#39;],</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  fs.writeFileSync(</span><br><span class="line">    &#96;$&#123;__dirname&#125;&#x2F;buildConfig.js&#96;, </span><br><span class="line">    &#96;module.exports &#x3D; $&#123;JSON.stringify(Object.assign(buildConfig, &#123;env&#125;), false, 2)&#125;&#96;,</span><br><span class="line">  )</span><br><span class="line"></span><br><span class="line">  return env</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#x2F;**</span><br><span class="line"> * 小程序代码上传</span><br><span class="line"> * </span><br><span class="line"> * @param &#123;String&#125; env 环境</span><br><span class="line"> *&#x2F;</span><br><span class="line">function commitCode (env) &#123;</span><br><span class="line">  return new Promise((resolve, reject) &#x3D;&gt; &#123;</span><br><span class="line">    const version &#x3D; packageConfig.version</span><br><span class="line">    const projectPath &#x3D; path.resolve(__dirname, &#39;..&#x2F;&#39;)</span><br><span class="line">    console.log(&quot;\n项目路径：\n\t -&gt;&quot;, projectPath, &#39;\n&#39;)</span><br><span class="line">    </span><br><span class="line">    const toolsPath &#x3D; buildConfig.toolsPath</span><br><span class="line"></span><br><span class="line">    const command &#x3D; &#96;cli $&#123;[</span><br><span class="line">      &#39;upload&#39;,</span><br><span class="line">      &#39;--project&#39;,</span><br><span class="line">      projectPath,</span><br><span class="line">      &#39;-v&#39;,</span><br><span class="line">      version,</span><br><span class="line">      &#39;-d&#39;,</span><br><span class="line">      &#96;$&#123;env&#125;-$&#123;version&#125;&#96;</span><br><span class="line">    ].join(&#39; &#39;)&#125;&#96;</span><br><span class="line"></span><br><span class="line">    console.log(&quot;小程序上传代码命令：\n\t -&gt;&quot;, command, &#39;\n&#39;)</span><br><span class="line">    &#x2F;&#x2F; const ls &#x3D; execSync(command, &#123;</span><br><span class="line">    &#x2F;&#x2F;   cwd: toolsPath,</span><br><span class="line">    &#x2F;&#x2F; &#125;)</span><br><span class="line"></span><br><span class="line">    &#x2F;&#x2F; console.log(ls, ls.toString())</span><br><span class="line"></span><br><span class="line">    &#x2F;&#x2F; console.log(Object.prototype.toString.call(ls))</span><br><span class="line"></span><br><span class="line">    &#x2F;&#x2F; console.log(ls.pid, ls.output, ls.stdout)</span><br><span class="line"></span><br><span class="line">    exec(command, &#123;cwd: toolsPath&#125;, (error, stdout, stderr) &#x3D;&gt; &#123;</span><br><span class="line">      &#x2F;&#x2F; console.log(&#96;error: $&#123;error&#125;&#96;);</span><br><span class="line">      &#x2F;&#x2F; console.log(&#96;stdout: $&#123;stdout&#125;&#96;);</span><br><span class="line">      &#x2F;&#x2F; console.error(&#96;stderr: $&#123;stderr&#125;&#96;);</span><br><span class="line">      if (error) &#123;</span><br><span class="line">        console.error(&#96;执行的错误: $&#123;error&#125;&#96;)</span><br><span class="line"></span><br><span class="line">        reject(error)</span><br><span class="line">        return;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      if (!stdout) &#123;</span><br><span class="line">        console.error(&#96;执行的错误: $&#123;stderr&#125;&#96;)</span><br><span class="line"></span><br><span class="line">        reject(stderr)</span><br><span class="line">        return;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      console.log(&#96;stdout: $&#123;stdout&#125;&#96;);</span><br><span class="line"></span><br><span class="line">      resolve()</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#x2F;**</span><br><span class="line"> * 获取上传版本号</span><br><span class="line"> * </span><br><span class="line"> * 开发者选择需要更新的版本号</span><br><span class="line"> * </span><br><span class="line"> * 未来版本是选择版本号+自定义版本号，比如当前是1.0.0给出如下几种方案进行选择：</span><br><span class="line"> * 1.0.0</span><br><span class="line"> * 1.0.1</span><br><span class="line"> * 1.1.0</span><br><span class="line"> * 2.0.0</span><br><span class="line"> * 自定义</span><br><span class="line"> * </span><br><span class="line"> * 选择自定义需要开发者自己手动输入，需要分三段输入，每次输入都要第一段校验正整数，后面两段检验是非负整数，并写入package.json</span><br><span class="line"> * </span><br><span class="line"> * 这种傻瓜式配置更能让人理解，而且扩展能力也比较好</span><br><span class="line"> *&#x2F;</span><br><span class="line">async function getVersion () &#123;</span><br><span class="line">  if (!packageConfig.version) &#123;</span><br><span class="line">    const version &#x3D; &#39;1.0.0&#39;</span><br><span class="line"></span><br><span class="line">    console.log(&quot;\n当前无版本号，使用默认版本号：\n\t -&gt;&quot;, version)</span><br><span class="line"></span><br><span class="line">    packageConfig.version &#x3D; version</span><br><span class="line"></span><br><span class="line">    return version</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  console.log(&quot;\n当前版本号：\n\t -&gt;&quot;, packageConfig.version, &#39;\n&#39;)</span><br><span class="line"></span><br><span class="line">  const versionTypeOpts &#x3D; [&#39;版本升级&#39;, &#39;特性更新&#39;, &#39;修订补丁&#39;]</span><br><span class="line"></span><br><span class="line">  const &#123;versionType&#125; &#x3D; await inquirer.prompt(&#123;</span><br><span class="line">    type: &#39;list&#39;,</span><br><span class="line">    name: &#39;versionType&#39;,</span><br><span class="line">    message: &#39;选择需要更新的版本类型：\n\t -&gt;&#39;,</span><br><span class="line">    default: &#39;修订补丁&#39;,</span><br><span class="line">    choices: versionTypeOpts,</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  const index &#x3D; versionTypeOpts.findIndex(item &#x3D;&gt; item &#x3D;&#x3D;&#x3D; versionType)</span><br><span class="line"></span><br><span class="line">  const version &#x3D; packageConfig.version.replace(&#x2F;(.+)\.(.+)\.(.+)&#x2F;, (...args) &#x3D;&gt; &#123;</span><br><span class="line">    const _version &#x3D; args.slice(1,4)</span><br><span class="line">    _version[index] &#x3D; Number(_version[index] || 0) + 1</span><br><span class="line"></span><br><span class="line">    return _version.join(&#39;.&#39;)</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  packageConfig.version &#x3D; version</span><br><span class="line"></span><br><span class="line">  return version</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#x2F;**</span><br><span class="line"> * 本地写入版本号</span><br><span class="line"> * </span><br><span class="line"> * 这里没有考虑使用版本号来区别是为了更好的灵活性</span><br><span class="line"> *&#x2F;</span><br><span class="line">function setVersion () &#123;</span><br><span class="line">  if (process.argv[3] !&#x3D;&#x3D; &#39;version&#39;) return</span><br><span class="line"></span><br><span class="line">  console.log(&quot;版本号写入：\n\t -&gt;&quot;, packageConfig.version, &#39;\n&#39;)</span><br><span class="line">  fs.writeFileSync(</span><br><span class="line">    path.resolve(__dirname, &#39;..&#x2F;package.json&#39;), </span><br><span class="line">    JSON.stringify(packageConfig, false, 2),</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br></pre>
                  </td>
                </tr>
              </table>
            </figure>
            <h1 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h1>
            <p>各个项目的环境名称各不相同，需要根据需求自己修改。</p>
            <p>buildConfig.js 默认配置成安装目录为空，默认环境为线上环境</p>
            <p>如果担心文件bei修改就加入.gitignore禁止上传</p>
            <h1 id="编译部分修改"><a href="#编译部分修改" class="headerlink" title="编译部分修改"></a>编译部分修改</h1>
            <p>代码的编译可以</p>
            <h1 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h1>
            <p><a target="_blank" rel="noopener" href="https://developers.weixin.qq.com/miniprogram/dev/devtools/cli.html">https://developers.weixin.qq.com/miniprogram/dev/devtools/cli.html</a><br><a target="_blank" rel="noopener" href="https://developers.weixin.qq.com/miniprogram/dev/devtools/http.html">https://developers.weixin.qq.com/miniprogram/dev/devtools/http.html</a><br><a target="_blank" rel="noopener" href="http://nodejs.cn/api/readline.html#readline_readline_createinterface_options">http://nodejs.cn/api/readline.html#readline_readline_createinterface_options</a><br><a target="_blank" rel="noopener" href="http://nodejs.cn/api/child_process.html#child_process_child_process_exec_command_options_callback">http://nodejs.cn/api/child_process.html#child_process_child_process_exec_command_options_callback</a><br><a target="_blank" rel="noopener" href="http://nodejs.cn/api/fs.html#fs_fs_writefilesync_file_data_options">http://nodejs.cn/api/fs.html#fs_fs_writefilesync_file_data_options</a><br><a target="_blank" rel="noopener" href="http://nodejs.cn/api/path.html#path_path_relative_from_to">http://nodejs.cn/api/path.html#path_path_relative_from_to</a><br><a target="_blank" rel="noopener" href="https://github.com/SBoudrias/Inquirer.js">https://github.com/SBoudrias/Inquirer.js</a><br><a target="_blank" rel="noopener" href="https://www.npmjs.com/package/miniprogram-ci">https://www.npmjs.com/package/miniprogram-ci</a><br><a target="_blank" rel="noopener" href="https://github.com/xlzy520/uniapp-tailwind-uview-starter">https://github.com/xlzy520/uniapp-tailwind-uview-starter</a></p>

          </div>





          <footer class="post-footer">
            <div class="post-tags">
              <a href="/tags/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F/" rel="tag"># 微信小程序</a>
              <a href="/tags/%E5%BC%80%E5%8F%91%E8%80%85%E5%B7%A5%E5%85%B7/" rel="tag"># 开发者工具</a>
              <a href="/tags/%E4%BB%A3%E7%A0%81%E4%B8%8A%E4%BC%A0/" rel="tag"># 代码上传</a>
              <a href="/tags/%E5%91%BD%E4%BB%A4%E8%A1%8C%E8%B0%83%E7%94%A8/" rel="tag"># 命令行调用</a>
              <a href="/tags/miniprogram-ci/" rel="tag"># miniprogram-ci</a>
            </div>



            <div class="post-nav">
              <div class="post-nav-item">
                <a href="/blob/2021-05-17-encryption-scheme-and-implementation-of-front-end-sensitive-data/" rel="prev" title="前端敏感数据加密方案及实现">
                  <i class="fa fa-chevron-left"></i> 前端敏感数据加密方案及实现
                </a>
              </div>
              <div class="post-nav-item">
                <a href="/blob/2021-06-07-new-features-of-javsscript-es11-&-es2020/" rel="next" title="JavsScript ES11&ES2020的新特性">
                  JavsScript ES11&ES2020的新特性 <i class="fa fa-chevron-right"></i>
                </a>
              </div>
            </div>
          </footer>
        </article>
      </div>







      <script>
        window.addEventListener('tabs:register', () => {
          let {
            activeClass
          } = CONFIG.comments;
          if (CONFIG.comments.storage) {
            activeClass = localStorage.getItem('comments_active') || activeClass;
          }
          if (activeClass) {
            const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
            if (activeTab) {
              activeTab.click();
            }
          }
        });
        if (CONFIG.comments.storage) {
          window.addEventListener('tabs:click', event => {
            if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
            const commentClass = event.target.classList[1];
            localStorage.setItem('comments_active', commentClass);
          });
        }
      </script>
    </div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


      <div class="copyright">
        &copy;
        <span itemprop="copyrightYear">2021</span>
        <span class="with-love">
          <i class="fa fa-heart"></i>
        </span>
        <span class="author" itemprop="copyrightHolder">holidaypenguin</span>
      </div>
      <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
      </div>

    </div>
  </footer>


  <script size="300" alpha="0.1" zIndex="-1" src="https://cdn.jsdelivr.net/npm/ribbon.js@1.0.2/dist/ribbon.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
  <script src="/js/love.js"></script>

  <script src="/js/development.js"></script>
  <script src="/js/utils.js"></script>
  <script src="/js/motion.js"></script>
  <script src="/js/schemes/muse.js"></script>
  <script src="/js/next-boot.js"></script>


  <script src="/js/local-search.js"></script>









  <script>
    (function() {
      function leancloudSelector(url) {
        url = encodeURI(url);
        return document.getElementById(url).querySelector('.leancloud-visitors-count');
      }

      function addCount(Counter) {
        const visitors = document.querySelector('.leancloud_visitors');
        const url = decodeURI(visitors.id);
        const title = visitors.dataset.flagTitle;

        Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({
            url
          })))
          .then(response => response.json())
          .then(({
            results
          }) => {
            if (results.length > 0) {
              const counter = results[0];
              leancloudSelector(url).innerText = counter.time + 1;
              Counter('put', '/classes/Counter/' + counter.objectId, {
                  time: {
                    '__op': 'Increment',
                    'amount': 1
                  }
                })
                .catch(error => {
                  console.error('Failed to save visitor count', error);
                });
            } else {
              Counter('post', '/classes/Counter', {
                  title,
                  url,
                  time: 1
                })
                .then(response => response.json())
                .then(() => {
                  leancloudSelector(url).innerText = 1;
                })
                .catch(error => {
                  console.error('Failed to create', error);
                });
            }
          })
          .catch(error => {
            console.error('LeanCloud Counter Error', error);
          });
      }

      function showTime(Counter) {
        const visitors = document.querySelectorAll('.leancloud_visitors');
        const entries = [...visitors].map(element => {
          return decodeURI(element.id);
        });

        Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({
            url: {
              '$in': entries
            }
          })))
          .then(response => response.json())
          .then(({
            results
          }) => {
            for (let url of entries) {
              const target = results.find(item => item.url === url);
              leancloudSelector(url).innerText = target ? target.time : 0;
            }
          })
          .catch(error => {
            console.error('LeanCloud Counter Error', error);
          });
      }

      const {
        app_id,
        app_key,
        server_url
      } = {
        "enable": true,
        "app_id": "povuqNsSqFodlakVIwtEX5kb-gzGzoHsz",
        "app_key": "zXD40RDtDB3DMtpC89k0AK7g",
        "server_url": null,
        "security": false
      };

      function fetchData(api_server) {
        const Counter = (method, url, data) => {
          return fetch(`${api_server}/1.1${url}`, {
            method,
            headers: {
              'X-LC-Id': app_id,
              'X-LC-Key': app_key,
              'Content-Type': 'application/json',
            },
            body: JSON.stringify(data)
          });
        };
        if (CONFIG.page.isPost) {
          if (CONFIG.hostname !== location.hostname) return;
          addCount(Counter);
        } else if (document.querySelectorAll('.post-title-link').length >= 1) {
          showTime(Counter);
        }
      }

      const api_server = app_id.slice(-9) === '-MdYXbMMI' ? `https://${app_id.slice(0, 8).toLowerCase()}.api.lncldglobal.com` : server_url;

      if (api_server) {
        fetchData(api_server);
      } else {
        fetch('https://app-router.leancloud.cn/2/route?appId=' + app_id)
          .then(response => response.json())
          .then(({
            api_server
          }) => {
            fetchData('https://' + api_server);
          });
      }
    })();
  </script>



<script>
            window.imageLazyLoadSetting = {
                isSPA: false,
                preloadRatio: 1,
                processImages: null,
            };
        </script><script>window.addEventListener("load",function(){var t=/\.(gif|jpg|jpeg|tiff|png)$/i,r=/^data:image\/[a-z]+;base64,/;Array.prototype.slice.call(document.querySelectorAll("img[data-original]")).forEach(function(a){var e=a.parentNode;"A"===e.tagName&&(e.href.match(t)||e.href.match(r))&&(e.href=a.dataset.original)})});</script><script>!function(n){n.imageLazyLoadSetting.processImages=o;var e=n.imageLazyLoadSetting.isSPA,i=n.imageLazyLoadSetting.preloadRatio||1,r=Array.prototype.slice.call(document.querySelectorAll("img[data-original]"));function o(){e&&(r=Array.prototype.slice.call(document.querySelectorAll("img[data-original]")));for(var t,a=0;a<r.length;a++)0<=(t=(t=r[a]).getBoundingClientRect()).bottom&&0<=t.left&&t.top<=(n.innerHeight*i||document.documentElement.clientHeight*i)&&function(){var t,e,n,i,o=r[a];t=o,e=function(){r=r.filter(function(t){return o!==t})},n=new Image,i=t.getAttribute("data-original"),n.onload=function(){t.src=i,e&&e()},t.src!==i&&(n.src=i)}()}o(),n.addEventListener("scroll",function(){var t,e;t=o,e=n,clearTimeout(t.tId),t.tId=setTimeout(function(){t.call(e)},500)})}(this);</script></body>

</html>